/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.una.accidentmanagement.bo;

import br.com.una.accidentmanagement.model.*;
import br.com.una.accidentmanagement.util.DatabaseConnection;
import br.com.una.accidentmanagement.util.JPAUtil;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.persistence.EntityManager;
import javax.persistence.TypedQuery;

/**
 *
 * @author emilianoeloi
 */
public class AreaController {
    
    public void createArea(Area area){
        try {
            JPAUtil.create(area);
        } catch (Exception ex) {
            Logger.getLogger(AreaController.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    public void createService(Service service){
        try {
            JPAUtil.create(service);
        } catch (Exception ex) {
            Logger.getLogger(AreaController.class.getName()).log(Level.SEVERE, null, ex);
        }    
    }

    public List<Area> getAreas() {
        EntityManager em = DatabaseConnection.instance().getManager();
        TypedQuery<Area> query = em.createQuery("SELECT a FROM Area a", Area.class);        
        return query.getResultList();
    }

    public List<Service> getServicesByArea(int areaId) {
        try {
            return JPAUtil.listByProperty(Service.class, "id", areaId);
        } catch (Exception ex) {
            Logger.getLogger(AreaController.class.getName()).log(Level.SEVERE, null, ex);
            return null;
        }       
    }
}
